local qianxiang = fk.CreateSkill{
  name = "xiaobai__qianxiang",
}
qianxiang:addEffect(fk.TurnEnd,{
  anim_type = "control",
  can_trigger = function (self, event, target, player, data)
    return player:getMark("@qianxiang_qian") >0 and player:getMark("@qianxiang_kun") >0 and 
      player:hasSkill(self)
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local difference = player:getMark("@qianxiang_qian") - player:getMark("@qianxiang_kun")
    if difference == 0 then 
      if room:askToSkillInvoke(player, {
        skill_name = qianxiang.name,
        prompt = "#do_change:::"..0
      }) then 
        room:throwCard(player:getCardIds(Player.Hand), qianxiang.name, player, player) 
      end 
      return 
    end
    if difference < 0 then
      difference = -difference
    end
    if difference >5 then difference = 5 end
    local cards = room:getNCards(difference,"top")
    local use = room:askToUseRealCard(player, {
      pattern = ".",
      expand_pile = cards,
      skill_name = qianxiang.name,
      prompt = "#qianxiang_ask:::"..difference..":::"..difference,
      extra_data = {bypass_time = true,extraUse = true,expand_pile = cards}
    })
    if not use and room:askToSkillInvoke(player, {skill_name = qianxiang.name, prompt = "#do_change:::"..difference}) then
      local num = difference - player:getHandcardNum()
      if num > 0 then
        player:drawCards(num, qianxiang.name)
      elseif num < 0 then
        room:askToDiscard(player,{ 
          min_num = -num, 
          max_num = -num,
          cancelable = false, 
          skill_name = qianxiang.name,
          include_equip = false})
      end
    end
  end,
})

qianxiang:addEffect(fk.AfterCardsMove, {
  can_refresh = function (self, event, target, player, data)
    return player:hasSkill(self, true)
  end,
  on_refresh = function (self, event, target, player, data)
    for _, move in ipairs(data) do
      if move.from == player then
        for _, info in ipairs(move.moveInfo) do
          if info.fromArea == Card.PlayerEquip or info.fromArea == Card.PlayerHand then
            player.room:addPlayerMark(player,"@qianxiang_qian")
          end
        end
      end
      if move.to == player and (move.toArea == Card.PlayerHand or move.toArea == Card.PlayerEquip)then
        for _, info in ipairs(move.moveInfo) do
          player.room:addPlayerMark(player,"@qianxiang_kun")
        end
      end
    end
  end
})
qianxiang:addEffect(fk.EventTurnChanging,{
  can_refresh = function (self, event, target, player, data)
    return player:hasSkill(self, true)
  end,
  on_refresh = function (self, event, target, player, data)
    player.room:setPlayerMark(player,"@qianxiang_qian",0)
    player.room:setPlayerMark(player,"@qianxiang_kun",0)
  end
})
qianxiang:addLoseEffect(function (self, player)
    player.room:setPlayerMark(player,"@qianxiang_qian",0)
    player.room:setPlayerMark(player,"@qianxiang_kun",0)
  end
)
qianxiang:addAcquireEffect(function (self, player)
  player.room.logic:getEventsOfScope(GameEvent.MoveCards, 1, function(e)
    for _, move in ipairs(e.data) do
      if move.from == player then
        for _, info in ipairs(move.moveInfo) do
          if info.fromArea == Card.PlayerEquip or info.fromArea == Card.PlayerHand then
            player.room:addPlayerMark(player,"@qianxiang_qian")
          end
        end
      end
      if move.to == player and (move.toArea == Card.PlayerHand or move.toArea == Card.PlayerEquip) then
        for _, info in ipairs(move.moveInfo) do
          player.room:addPlayerMark(player,"@qianxiang_kun")
        end
      end
    end
  end, Player.HistoryTurn)
end)

Fk:loadTranslationTable{
  ["xiaobai__qianxiang"] = "乾象",
  [":xiaobai__qianxiang"] = "一名角色回合结束时，若你本回合获得且失去过牌，"..
    "你观看牌堆顶X张牌并可以使用其中一张，若你未以此法使用牌，则你可将手牌数调整为X（X为本回合你获得与失去牌数的差值且至多为5）。",
  ["@qianxiang_qian"] = "失去",
  ["@qianxiang_kun"] = "获得",
  ["#qianxiang_ask"] = "乾象：你可以使用牌堆顶 %arg 张牌中的一张，或点“取消”然后选择是否将手牌调整至 %arg 张。",
  ["#do_change"] = "坤象：是否将手牌调整至 %arg 张",
  ["$xiaobai__qianxiang1"] = "三光之行，不必有常，术家以算求之。",
  ["$xiaobai__qianxiang2"] = "分黄赤、推两仪，是知天体如弹丸。"
}

return qianxiang